package com.df.bpm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.df.bpm.dto.DtoRoleCase;
import com.df.bpm.entity.Business;
import com.df.bpm.entity.RoleCase;
import com.df.bpm.service.impl.*;
import com.df.common.entity.Pagination;
import com.df.common.utils.CommonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/**
 * 业务控制器
 */
@Slf4j
@Tag(name = "业务控制器")
@RequestMapping("/business")
@RequiredArgsConstructor
@RestController
@SaCheckLogin
public class BusinessController {
    private final BusinessServiceImpl businessServiceImpl;
    private final DtoRoleCaseServiceImpl dtoRoleCaseServiceImpl;
    private final RoleCaseServiceImpl roleCaseServiceImpl;

    /**
     * 查询业务
     * @param pagination 分页信息
     * @param temp 业务线索
     * @return  业务列表
     */
    @Operation(summary = "查询业务", description = "查询业务")
    @GetMapping("")
    @SaIgnore
    public ResponseEntity<?> getBusiness(Pagination pagination, Business temp) {
        if (CommonUtils.varIsNotBlank(pagination.getPageNum(), pagination.getPageSize())) {
            PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
            return ResponseEntity.ok(new PageInfo<>(businessServiceImpl.getList4Like(temp)));
        } else {
            return ResponseEntity.ok(businessServiceImpl.getList4Like(temp));
        }
    }

    /**
     * 新增业务
     * @param temp   角色信息
     * @return  新增成功后的业务信息
     */
    @Operation(summary = "新增业务", description = "新增业务")
    @PostMapping("")
    @SaIgnore
    public ResponseEntity<?> newBusiness(@RequestBody Business temp) {
        businessServiceImpl.add(temp);
        return ResponseEntity.ok(temp);
    }

    /**
     * 查询角色业务dto
     * @param pagination 分页信息
     * @param temp 角色业务dto线索
     * @return  角色业务dto列表
     */
    @Operation(summary = "查询角色业务dto", description = "查询角色业务dto")
    @GetMapping("dtoRoleCase")
    @SaIgnore
    public ResponseEntity<?> getDtoRoleCase(Pagination pagination, DtoRoleCase temp) {
        if (CommonUtils.varIsNotBlank(pagination.getPageNum(), pagination.getPageSize())) {
            PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
            return ResponseEntity.ok(new PageInfo<>(dtoRoleCaseServiceImpl.getList4Like(temp)));
        } else {
            return ResponseEntity.ok(dtoRoleCaseServiceImpl.getList4Like(temp));
        }
    }

    /**
     * 创建角色业务
     * @param temp   角色业务信息
     * @return  新增成功后的业务信息
     */
    @Operation(summary = "创建角色业务", description = "创建角色业务")
    @PostMapping("roleBusiness")
    @SaIgnore
    public ResponseEntity<?> newRoleBusiness(@RequestBody DtoRoleCase temp) {
        Business newBusiness=new Business();
        if(temp==null || temp.getName()==null){
            throw new RuntimeException("新业务名不能为空");
        }else{
            newBusiness.setName(temp.getName());
            businessServiceImpl.add(newBusiness);
            RoleCase newShip=new RoleCase();
            newShip.setBusinessId(newBusiness.getId());
            newShip.setRoleId(temp.getRoleId());
            roleCaseServiceImpl.add(newShip);
            return ResponseEntity.ok(newBusiness);
        }
    }

}
